﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.Common;
using System.Text;
using Microsoft.Practices.EnterpriseLibrary.Data;
using DuJianliang.ApplicationPlatform.Common;

namespace DuJianliang.ApplicationPlatform.Proj5715.Process
{
    public partial class SelectMissile : System.Web.UI.Page
    {
        private string DispatchID
        {
            get
            {
                object obj = ViewState["DispatchID"];
                return obj != null ? obj.ToString() : string.Empty;
            }
            set
            {
                ViewState["DispatchID"] = value;
            }
        }

        private string MissileModel
        {
            get
            {
                object obj = ViewState["MissileModel"];
                return obj != null ? obj.ToString() : string.Empty;
            }
            set
            {
                ViewState["MissileModel"] = value;
            }
        }

        private string Level
        {
            get
            {
                object obj = ViewState["Level"];
                return obj != null ? obj.ToString() : string.Empty;
            }
            set
            {
                ViewState["Level"] = value;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                this.DispatchID = Request.QueryString["DispatchID"];
                this.MissileModel = Request.QueryString["MissileModel"];
                this.Level = Request.QueryString["level"];

                BindGridView1();
                BindGridView2();
            }
        }

        private void BindGridView1()
        {
            ProcessDal dal = new ProcessDal();
            DataTable dt = dal.GetMissileTodoList(this.MissileModel, this.Level);
            GridView1.DataSource = dt;
            GridView1.DataBind();
        }

        private void BindGridView2()
        {
            ProcessDal dal = new ProcessDal();
            DataTable dt = dal.GetDispatchMissileTable(this.DispatchID);
            GridView2.DataSource = dt;
            GridView2.DataBind();
        }

        protected void btnOK_Click(object sender, EventArgs e)
        {
            //StringBuilder processIDs = new StringBuilder();

            //foreach (GridViewRow gridViewRow in GridView1.Rows)
            //{
            //    if (gridViewRow.RowType != DataControlRowType.DataRow)
            //    {
            //        continue;
            //    }

            //    CheckBox checkBox = gridViewRow.Cells[5].FindControl("ckbSelect") as CheckBox;
            //    if (checkBox == null)
            //    {
            //        continue;
            //    }

            //    if (checkBox.Checked)
            //    {
            //        processIDs.Append(GridView1.DataKeys[gridViewRow.RowIndex]["distnumber"].ToString() + "|");
            //    }
            //}

            //Response.Write(processIDs.ToString().TrimEnd('|'));

            Response.Redirect(string.Format("DispatchEdit.aspx?flowInstanceID={0}&Source=list", this.DispatchID));
        }

        protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (string.IsNullOrWhiteSpace(e.CommandName) || e.CommandName.ToLower().Equals("page"))
            {
                return;
            }

            GridViewRow gridViewRow = ((GridViewRow)(((Button)(e.CommandSource)).Parent.Parent));
            DispatchMissileOB dispatchMissileOB = new DispatchMissileOB();
            dispatchMissileOB.DispatchMissileID = Guid.NewGuid().ToString();
            dispatchMissileOB.DispatchID = this.DispatchID;
            dispatchMissileOB.SysNumber = GridView1.DataKeys[gridViewRow.RowIndex]["SysNumber"].ToString();
            dispatchMissileOB.MissileModel = GridView1.Rows[gridViewRow.RowIndex].Cells[0].Text;
            dispatchMissileOB.MissileCode = GridView1.Rows[gridViewRow.RowIndex].Cells[1].Text;
            dispatchMissileOB.InDate = GridView1.Rows[gridViewRow.RowIndex].Cells[2].Text;
            dispatchMissileOB.RemainingLife = GridView1.Rows[gridViewRow.RowIndex].Cells[3].Text;
            dispatchMissileOB.IsCompleted = "0";
            
            switch (e.CommandName)
            {
                case "CmdSelect":
                    ProcessDal dal = new ProcessDal();
                    dal.DispatchMissile_Insert(dispatchMissileOB);
                    BindGridView1();
                    BindGridView2();
                    break;                
                default:
                    break;
            }
        }

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType != DataControlRowType.DataRow)
            {
                return;
            }

            string strInDate = GridView1.DataKeys[e.Row.RowIndex]["InDate"].ToString();
            string strLifeTime = GridView1.DataKeys[e.Row.RowIndex]["RemainingLife"].ToString();

            DateTime inDate = DateTime.Now;
            if (!DateTime.TryParse(strInDate, out inDate))
            {
                return;
            }

            int lifeTime = 0;
            if (!int.TryParse(strLifeTime, out lifeTime))
            {
                return;
            }

            TimeSpan timeSpan = DateTime.Now.Subtract(inDate);
            double temp = timeSpan.Days / 30.00;
            double newLifeTime = lifeTime - temp;
            if (newLifeTime < 0)
            {
                newLifeTime = 0;
            }
            e.Row.Cells[3].Text = Math.Round(newLifeTime, 2).ToString();
        }

        protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType != DataControlRowType.DataRow)
            {
                return;
            }

            string strInDate = GridView2.DataKeys[e.Row.RowIndex]["InDate"].ToString();
            string strLifeTime = GridView2.DataKeys[e.Row.RowIndex]["RemainingLife"].ToString();

            DateTime inDate = DateTime.Now;
            if (!DateTime.TryParse(strInDate, out inDate))
            {
                return;
            }

            int lifeTime = 0;
            if (!int.TryParse(strLifeTime, out lifeTime))
            {
                return;
            }

            TimeSpan timeSpan = DateTime.Now.Subtract(inDate);
            double temp = timeSpan.Days / 30.00;
            double newLifeTime = lifeTime - temp;
            if (newLifeTime < 0)
            {
                newLifeTime = 0;
            }
            e.Row.Cells[3].Text = Math.Round(newLifeTime, 2).ToString();
        }        
    }
}